package com.haitaoit.chengquan.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.haitaoit.chengquan.base.ExitApplication;
import com.haitaoit.chengquan.module.car.activity.OrderPayActivity;
import com.haitaoit.chengquan.module.center.activity.xuqiu.MyXuqiuBaojiaActivity;
import com.haitaoit.chengquan.module.center.activity.xuqiu.MyXuqiuEndDetailActivity;
import com.haitaoit.chengquan.module.center.activity.xuqiu.PaySuccessActivity;
import com.haitaoit.chengquan.module.center.activity.xuqiu.PaymentActivity;
import com.haitaoit.chengquan.module.home.activity.maintenance.ServerPayActivity;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constant.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.e("tag", "onPayFinish, errCode = " + resp.errCode + "---" + resp.getType());
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            Log.e("tag", "支付结果:" + resp.errCode);
            if (resp.errCode == 0) {// 支付成功
                Toast.makeText(this, "温馨提示：支付成功", Toast.LENGTH_SHORT).show();
                if (PreferenceUtils.getPrefString("pay_type", "").equals("car_order")) {
                    Intent intent = new Intent(WXPayEntryActivity.this, PaySuccessActivity.class);
                    intent.putExtra("type", "car_order");
                    startActivity(intent);
                    ExitApplication.getInstance().finishActivity(OrderPayActivity.class);
                } else if (PreferenceUtils.getPrefString("pay_type", "").equals("xiuqiu_order")) {
                    Intent intent = new Intent(WXPayEntryActivity.this, PaySuccessActivity.class);
                    intent.putExtra("type", "xiuqiu_order");
                    startActivity(intent);
                    ExitApplication.getInstance().finishActivity(PaymentActivity.class);
                    if (PreferenceUtils.getPrefString("type", "").equals("1")) {
                        ExitApplication.getInstance().finishActivity(MyXuqiuEndDetailActivity.class);
                    } else if (PreferenceUtils.getPrefString("type", "").equals("2")) {
                        ExitApplication.getInstance().finishActivity(MyXuqiuBaojiaActivity.class);
                        ExitApplication.getInstance().finishActivity(MyXuqiuEndDetailActivity.class);
                    }
                } else if (PreferenceUtils.getPrefString("pay_type", "").equals("fuwu")) {
                    Intent intent = new Intent(WXPayEntryActivity.this, PaySuccessActivity.class);
                    intent.putExtra("type", "fuwu");
                    startActivity(intent);
                    ExitApplication.getInstance().finishActivity(ServerPayActivity.class);
                }
            } else if (resp.errCode == -2) {
                Toast.makeText(this, "温馨提示：取消支付", Toast.LENGTH_SHORT).show();
            } else if (resp.errCode == -1) {
                Toast.makeText(this, "温馨提示：支付失败", Toast.LENGTH_SHORT).show();
            }
        }
        finish();
    }
}
